package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.MarketCategoryService;
import com.cskaoyan.market.service.impl.MarketCategoryServiceImpl;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.CategoryUpdateVo;
import com.cskaoyan.market.vo.CategoryVo;
import com.cskaoyan.market.vo.LabelValueVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("admin/category")
public class AdminCategoryController {
    @Autowired
    MarketCategoryService categoryService;


    @GetMapping("list")
    private Object list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<CategoryVo> list = categoryService.list();
        Object o = ResponseUtil.okList(list);
        return o;
    }

    @GetMapping("l1")
    private Object l1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<LabelValueVo> list = categoryService.l1();
        return ResponseUtil.okList(list);
    }


    @PostMapping("create")
    private Object create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        MarketCategory category = null;

        //进行数据校验
        try {
            category = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCategory.class);
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgumentValue()));
        }
        category.setAddTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setDeleted(false);
        int resCode = categoryService.create(category);
        if (resCode == 1) {
            return ResponseUtil.ok(category);
        } else {
            return ResponseUtil.fail(-1, "类目添加失败");
        }
    }
    @PostMapping("update")
    private Object update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        int result;
        try {
            MarketCategory category = objectMapper.readValue(requestBody, MarketCategory.class);
            result = categoryService.update(category);
            if (result == 0) {
                return ResponseUtil.fail();
            }
            return ResponseUtil.ok();
        } catch (Exception e) {
            try {
                CategoryVo categoryVo = objectMapper.readValue(requestBody, CategoryVo.class);
                result = categoryService.update(categoryVo);
                if (result == 0) {
                    return ResponseUtil.fail();
                }
                return ResponseUtil.ok();
            } catch (Exception e1) {
                try {
                    CategoryUpdateVo categoryUpdateVo = objectMapper.readValue(requestBody, CategoryUpdateVo.class);
                    result = categoryService.update(categoryUpdateVo);
                    if (result == 0) {
                        return ResponseUtil.fail();
                    }
                    return ResponseUtil.ok();
                } catch (Exception e2) {
                    return ResponseUtil.badArgument();
                }
            }
        }
    }

    @PostMapping("delete")
    private Object delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        String idParam = JacksonUtil.parseString(requestBody, "id");
        if (idParam.equals("") || idParam == null) {
            return ResponseUtil.badArgument();
        }
        int id = 0;
        try {
            id = Integer.parseInt(idParam);
        } catch (NumberFormatException e) {
            return ResponseUtil.badArgument();
        }
        int result = categoryService.delete(id);
        if (result == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }
}